package l1j.server.server;

import static l1j.server.server.model.skill.L1SkillId.ADDITIONAL_FIRE;
import static l1j.server.server.model.skill.L1SkillId.ADVANCE_SPIRIT;
import static l1j.server.server.model.skill.L1SkillId.AQUA_PROTECTER;
import static l1j.server.server.model.skill.L1SkillId.BERSERKERS;
import static l1j.server.server.model.skill.L1SkillId.BLESS_WEAPON;
import static l1j.server.server.model.skill.L1SkillId.BOUNCE_ATTACK;
import static l1j.server.server.model.skill.L1SkillId.BRAVE_AURA;
import static l1j.server.server.model.skill.L1SkillId.BURNING_SPIRIT;
import static l1j.server.server.model.skill.L1SkillId.BURNING_WEAPON;
import static l1j.server.server.model.skill.L1SkillId.CLEAR_MIND;
import static l1j.server.server.model.skill.L1SkillId.DECREASE_WEIGHT;
import static l1j.server.server.model.skill.L1SkillId.DOUBLE_BRAKE;
import static l1j.server.server.model.skill.L1SkillId.DRESS_EVASION;
import static l1j.server.server.model.skill.L1SkillId.ELEMENTAL_FIRE;
import static l1j.server.server.model.skill.L1SkillId.ELEMENTAL_PROTECTION;
import static l1j.server.server.model.skill.L1SkillId.ENCHANT_VENOM;
import static l1j.server.server.model.skill.L1SkillId.EXOTIC_VITALIZE;
import static l1j.server.server.model.skill.L1SkillId.GLOWING_AURA;
import static l1j.server.server.model.skill.L1SkillId.IMMUNE_TO_HARM;
import static l1j.server.server.model.skill.L1SkillId.IRON_SKIN;
import static l1j.server.server.model.skill.L1SkillId.LIGHT;
import static l1j.server.server.model.skill.L1SkillId.MEDITATION;
import static l1j.server.server.model.skill.L1SkillId.PHYSICAL_ENCHANT_DEX;
import static l1j.server.server.model.skill.L1SkillId.PHYSICAL_ENCHANT_STR;
import static l1j.server.server.model.skill.L1SkillId.REDUCTION_ARMOR;
import static l1j.server.server.model.skill.L1SkillId.RESIST_MAGIC;
import static l1j.server.server.model.skill.L1SkillId.SOLID_CARRIAGE;
import static l1j.server.server.model.skill.L1SkillId.SOUL_OF_FLAME;
import static l1j.server.server.model.skill.L1SkillId.UNCANNY_DODGE;
import static l1j.server.server.model.skill.L1SkillId.VENOM_RESIST;
import static l1j.server.server.model.skill.L1SkillId.WATER_LIFE;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import l1j.server.server.datatables.ItemTable;
import l1j.server.server.datatables.NpcTable;
import l1j.server.server.model.L1Inventory;
import l1j.server.server.model.L1Location;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.L1Party;
import l1j.server.server.model.L1Teleport;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.map.L1Map;
import l1j.server.server.model.map.L1WorldMap;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.serverpackets.S_SystemMessage;
import java.util.Random;
import l1j.server.server.utils.collections.Lists;

public class SurvivalBattleController implements Runnable {

	private static SurvivalBattleController _instance;
	
	public static boolean _battleOn = false;
	
	public static boolean _battleStart = false;
	
	public static SurvivalBattleController getInstance() {
		if (_instance == null) {
			_instance = new SurvivalBattleController();
		}
		return _instance;
	}
	
	public SurvivalBattleController() {
		_pc.clear();
		_gateId[0] = -1;
		_gateId[1] = -1;
		_gateId[2] = -1;
		_gateId[3] = -1;
		_gateId[4] = -1;
		_gateId[5] = -1;
		_stageClear[0] = -1;
		_stageClear[1] = -1;
		_stageClear[2] = -1;
		obj1 = null;
		obj2 = null;
	}
	
	private GregorianCalendar _cgc;
	
	private boolean checkTime() {
		_cgc = new GregorianCalendar();
		if (_cgc.get(Calendar.HOUR_OF_DAY) >= 20 && _cgc.get(Calendar.HOUR_OF_DAY) <= 22) {
			return true;
		}
		return false;
	}
	
	public static L1Party pt = new L1Party();
	
	public static List<L1PcInstance> _pc = Lists.newList();
	
	private boolean checkPlayerNum() {
		int count = 0;
		if (pt!=null) {
			for (L1PcInstance l1pc : L1World.getInstance().getAllPlayers()) {
				if (l1pc.getMapId()==(short)511 && _pc.contains(l1pc)) {
					count++;
				}
			}
			if (count==8 && _pc.size()==8) {	//副本限制8人开始
				return true;
			}
		}
		return false;
	}
	
	private static int _npcid = 0;
	
	private void spwanNpc() {
		if (_npcid == 0) {
			L1NpcInstance npc = NpcTable.getInstance().newNpcInstance(909999);
			_npcid = IdFactory.getInstance().nextId();
			npc.setId(_npcid);
			npc.setX(34061);
			npc.setY(32277);
			npc.setMap((short)4);
			npc.setHeading(5);
			L1World.getInstance().storeObject(npc);
			L1World.getInstance().addVisibleObject(npc);
			npc.turnOnOffLight();
		}
	}
	//不在副本可进行时间，移除副本管理员
	private void removeNpc() {
		L1Object obj = L1World.getInstance().findObject(_npcid);
		if (obj!=null) {
			if (obj instanceof L1NpcInstance) {
				L1NpcInstance l1npc = (L1NpcInstance) obj;
				l1npc.deleteMe();
				_npcid = 0;
			}
		}
	}
	
	private static int _c = 0;
	
	private void goback() {
		_c = 0;
		for (L1PcInstance pc : L1World.getInstance().getAllPlayers()) {
			//检查GM
			if (pc.getMapId()==(short)511 && pc.getAccessLevel()>0) {
				for (L1ItemInstance it : pc.getInventory().getItems()) {
					if (it.getItemId()>=70015 && it.getItemId()<=70018) {
						pc.getInventory().removeItem(it, it.getCount());
					}
				}
				L1Teleport.teleport(pc, 34056, 32277, (short) 4, 0, true);
			} else if (pc.getMapId()==(short)511) {
				_c++;
			}
		}
		//副本人员超出8人限制
		if (_c > 8) {
			for (L1PcInstance pc : L1World.getInstance().getAllPlayers()) {
				if (pc.getMapId()==(short)511 && !_pc.contains(pc)) {
					for (L1ItemInstance it : pc.getInventory().getItems()) {
						if (it.getItemId()>=70015 && it.getItemId()<=70020) {
							pc.getInventory().removeItem(it, it.getCount());
						}
					}
					L1Teleport.teleport(pc, 34056, 32277, (short) 4, 0, true);
				}
			}
			clearFB();
		}
		//检查GM buffer
		for (L1PcInstance pc : L1World.getInstance().getAllPlayers()) {
			if (pc.getMapId()==(short)511 && _pc.contains(pc) && pc.getTempCharGfx()==5641) {
				int[] allBuffSkill =
					{ LIGHT, DECREASE_WEIGHT, PHYSICAL_ENCHANT_DEX, MEDITATION, PHYSICAL_ENCHANT_STR, BLESS_WEAPON, BERSERKERS, IMMUNE_TO_HARM, ADVANCE_SPIRIT,
							REDUCTION_ARMOR, BOUNCE_ATTACK, SOLID_CARRIAGE, ENCHANT_VENOM, BURNING_SPIRIT, VENOM_RESIST, DOUBLE_BRAKE, UNCANNY_DODGE,
							DRESS_EVASION, GLOWING_AURA, BRAVE_AURA, RESIST_MAGIC, CLEAR_MIND, ELEMENTAL_PROTECTION, AQUA_PROTECTER, BURNING_WEAPON, IRON_SKIN,
							EXOTIC_VITALIZE, WATER_LIFE, ELEMENTAL_FIRE, SOUL_OF_FLAME, ADDITIONAL_FIRE };
				for (int j : allBuffSkill) {
					if (pc.hasSkillEffect(j)) {
						pc.removeSkillEffect(j);
					}
				}
			}
		}
	}
	//结束副本
	private void endIt() {
		for (L1PcInstance pc : L1World.getInstance().getAllPlayers()) {
			if (pc.getMapId()==(short)511) {
				for (L1ItemInstance it : pc.getInventory().getItems()) {
					if (it.getItemId()>=70015 && it.getItemId()<=70018) {
						pc.getInventory().removeItem(it, it.getCount());
					}
				}
				L1Teleport.teleport(pc, 34056, 32277, (short) 4, 0, true);
			}
		}
		clearFB();
	}
	//生成传送门
	public static int [] _gateId = new int[6]; //0=stage1脱出 1=stage1下一阶段
												//2=stage2脱出 3=stage1下一阶段
												//4=stage3脱出 5=stage1下一阶段
	
	private static int i = 0;
	//type=0 脱出 1 下一阶段
	public static void spwanGate(int npcid, int type, short mapid) {
		i = 0;
		L1NpcInstance npc = NpcTable.getInstance().newNpcInstance(npcid);
		i = IdFactory.getInstance().nextId();
		npc.setId(i);
		switch (mapid) {
			case 511:
				if (type==0) { //脱出点 坐上
					npc.setX(32614);
					npc.setY(32804);
					if (_gateId[0]==-1) {
						_gateId[0] = i;
					}
				} else if (type==1) {	//下阶段 右上
					npc.setX(32610);
					npc.setY(32811);
					if (_gateId[1]==-1) {
						_gateId[1] = i;
					}
				}
				npc.setMap((short) 511);
				break;
			case 507:
				break;
			case 518:
				break;
		}
		npc.setHeading(0);
		L1World.getInstance().storeObject(npc);
		L1World.getInstance().addVisibleObject(npc);
		npc.turnOnOffLight();
		clearMon();
	}
	
	//清除副本
	private static void clearFB() {
		L1Object obj = null;
		L1NpcInstance npc = null;
		for (int i=0; i<_gateId.length; i++) {
			if (_gateId[i]!=-1) {
				obj = L1World.getInstance().findObject(_gateId[i]);
				if (obj!=null) {
					if (obj instanceof L1NpcInstance) {
						npc = (L1NpcInstance) obj;
						npc.deleteMe();
					}
				}
			}
		}
		clearMon();
		for (int i=0; i<_pc.size(); i++) {
			if (L1World.getInstance().findObject(_pc.get(i).getId())!=null) {
				for (L1ItemInstance it : _pc.get(i).getInventory().getItems()) {
					if (it.getItemId()>=70015 && it.getItemId()<=70018) {
						_pc.get(i).getInventory().removeItem(it, it.getCount());
					}
				}
			}
		}
		_battleStart = false;
		_c = 0;
		_loopTimer = 0;
		_n = 0;
		_npcid = 0;
		_pauseSpawn = false;
		_pc.clear();
		_readyTimer = 25;
		_randomLoc.clear();
		_spawnLoc.clear();
		_totalTimer = 1800;
		_monObj.clear();
		_gateId[0] = -1;
		_gateId[1] = -1;
		_gateId[2] = -1;
		_gateId[3] = -1;
		_gateId[4] = -1;
		_gateId[5] = -1;
		i = 0;
		_stageClear[0] = -1;
		_stageClear[1] = -1;
		_stageClear[2] = -1;
		_bossCount = 0;
		tobj = null;
		tnpc = null;
		_pTimer = 300;
	}
	//物品生成 0=etcitem 1=weapon 2=armor
	public static boolean createItem(L1PcInstance pc, int item_id, int count, int type, int enchant) {
		L1ItemInstance item = ItemTable.getInstance().createItem(item_id);
		if (item != null) {
			item.setCount(count);
			if (type==1 || type==2) {
				if (enchant<0) {
					enchant = 0;
				}
				item.setEnchantLevel(enchant);
			}
			if (pc.getInventory().checkAddItem(item, count) == L1Inventory.OK) {
				pc.getInventory().storeItem(item);
			}
			else {
				L1World.getInstance().getInventory(pc.getX(), pc.getY(), pc.getMapId()).storeItem(item);
			}
			pc.sendPackets(new S_ServerMessage(403, item.getLogName())); // %0を手に入れました。
			return true;
		}
		else {
			return false;
		}
	}
	
	//怪物等级数组
	private static int [] _lv1 = {901002, 901003, 901009, 901010};
	
	private static int [] _lv2 = {901004, 901007, 901008, 901011};
	
	private static int [] _lv3 = {901006, 901012};
	
	private static int [] _assistant = {901005};
	
	private static List<L1Location> _randomLoc = Lists.newList();	//每次生成的随机坐标
	
	private static List<L1Location> _spawnLoc = Lists.newList();	//触发生怪的坐标
	
	private static L1Map _map = L1WorldMap.getInstance().getMap((short)511);
	
	//随机坐标生成
	private void renderLoc() {
		_randomLoc.clear();
		int count = 0;
		int retry = 0;
		if (_map==null) {return;}
		while (count < 480) {	//生成450个点 几率达到450/600=75%
			L1Location loc = new L1Location();
			do {
//				loc.setX(new Random().nextInt(31)+32593);	//低限制
//				loc.setY(new Random().nextInt(58)+32738);	//低限制
				
				loc.setX(new Random().nextInt(16)+32601);	//高限制
				loc.setY(new Random().nextInt(37)+32757);	//高限制
				loc.setMap(511);
				if ((_map.isInMap(loc) || _map.isPassable(loc))
						&& !_randomLoc.contains(loc)) {
						_randomLoc.add(loc);
					count++;
					break;
				}
				retry++;
			} while (retry<50);
		}
	}
	//判断玩家是否在随机位置
	private void judgeLoc() {
		_spawnLoc.clear();
		if (_pc.size()>0) {
			for (L1PcInstance pc : _pc) {
				if (_randomLoc.contains(pc.getLocation())) {
					_spawnLoc.add(pc.getLocation());
				}
			}
		}
	}
	public static int _bossCount = 0;
	//开始生成怪物
	private void spawnMon() {
		int _base = 2; //基数 用于每次生成的怪物数量控制,最少为1
		int _boss = 100;
		int _ass = 0;
		int _i = 0;
		int _j = 0;
		int _m = 0;
		int _up = 0;
		int _loc = 0;
		
		if (_spawnLoc.size()>0) {
			_boss = new Random().nextInt(100);	//boss几率
			_ass = new Random().nextInt(100);	//辅助型怪物几率
//			_base = 2; //基数1只，最少为1
			
			if (_spawnLoc.size()>0 && _spawnLoc.size()<=3) {	
				if (_ass < 10) {
					_j = _base - 1;
					_ass = 1;
				} else {
					_j = _base;
					_ass = 0;
				}
				if (_boss < 20 && _bossCount<2) {	//BOSS数量控制
					_j = _j-1;
					_boss = 1;
				} else {
					_boss = 0;
				}
				_up = 0;
			} else if (_spawnLoc.size()>3 && _spawnLoc.size()<=5) {
				if (_ass < 5) {
					_j = _base - 1;
					_ass = 1;
				} else {
					_j = _base;
					_ass = 0;
				}
				if (_boss < 7 && _bossCount<2) {	//BOSS数量控制
					_j = _j-1;
					_boss = 1;
				} else {
					_boss = 0;
				}
				_up = 1;
			} else if (_spawnLoc.size()>5) {
				if (_ass < 10) {
					_j = _base - 1;
					_ass = 1;
				} else {
					_j = _base;
					_ass = 0;
				}
				if (_boss < 4 && _bossCount<2) {	//BOSS数量控制
					_j = _j-1;
					_boss = 1;
				} else {
					_boss = 0;
				}
				_up = 1;
			}
			if (_up == 0) {
				for (_i=0; _i<_j; _i++) {	//普通怪lv1
					_m = new Random().nextInt(_lv1.length);
					_loc = new Random().nextInt(_spawnLoc.size());
					spawnUtil(_lv1[_m], 1, _spawnLoc.get(_loc).getX()+new Random().nextInt(2)+1, 
							_spawnLoc.get(_loc).getY()+new Random().nextInt(2)+1);
				}
			} else if (_up == 1) {
				for (_i=0; _i<_j; _i++) {	//普通怪lv2
					_m = new Random().nextInt(_lv2.length);
					_loc = new Random().nextInt(_spawnLoc.size());
					spawnUtil(_lv2[_m], 1, _spawnLoc.get(_loc).getX()+new Random().nextInt(2)+1, 
							_spawnLoc.get(_loc).getY()+new Random().nextInt(2)+1);
				}
			}
			for (_i=0; _i<_ass; _i++) {	//辅助魔法型
				_m = new Random().nextInt(_assistant.length);
				_loc = new Random().nextInt(_spawnLoc.size());
				spawnUtil(_assistant[_m], 1, _spawnLoc.get(_loc).getX()+new Random().nextInt(2)+1, 
						_spawnLoc.get(_loc).getY()+new Random().nextInt(2)+1);
			}
			for (_i=0; _i<_boss; _i++) {	//BOSS
				_m = new Random().nextInt(_lv3.length);
				_loc = new Random().nextInt(_spawnLoc.size());
				spawnUtil(_lv3[_m], 1, _spawnLoc.get(_loc).getX()+new Random().nextInt(2)+1, 
						_spawnLoc.get(_loc).getY()+new Random().nextInt(2)+1);
			}
		}
	}
	//BOSS数量控制
	private boolean bossCount() {
		L1Object obj = null;
		L1NpcInstance npc = null;
		_bossCount = 0;
		for (int i=0; i<_monObj.size(); i++) {
			obj = L1World.getInstance().findObject(_monObj.get(i));
			if (obj instanceof L1NpcInstance) {
				npc = (L1NpcInstance) obj;
				if (npc.getNpcId()==901006 || npc.getNpcId()==901012) {
					_bossCount++;
				}
			}
		}
		if (_bossCount >= 2) {
			return true;
		}
		return false;
	}
	
	//副本开始后 检查玩家位置合法性
	private boolean checkInvalidLoc() {
		if (_stageClear[0] == 1) {
			return true;
		}
		for (L1PcInstance pc : _pc) {
			if (pc.getMapId()==511) {
				if ((pc.getX()<32600 || pc.getX()>32616) || (pc.getY()<32756 || pc.getY()>32793)) {	//高限制
//				if ((pc.getX()<32592 || pc.getX()>32623) || (pc.getY()<32737 || pc.getY()>32795)) {	//低限制
					return true;
				}
			}
		}
		return false;
	}
	//怪物生成工具
	private void spawnUtil(int monid, int count, int x, int y) {
		L1NpcInstance npc = NpcTable.getInstance().newNpcInstance(monid);
		int i =IdFactory.getInstance().nextId();
		_monObj.add(i);
		npc.setId(i);
		npc.setX(x);
		npc.setY(y);
		npc.setMap((short)511);
		npc.setHeading(0);
		npc.setHomeX(npc.getX());
		npc.setHomeY(npc.getY());
		L1World.getInstance().storeObject(npc);
		L1World.getInstance().addVisibleObject(npc);
		npc.turnOnOffLight();
	}
	//结束副本
	private static int _n = 0;
	
	private boolean closeFB() {
		_n = 0;
		//加入副本地图中已经没有玩家，结束副本
		for (L1PcInstance l1pc : L1World.getInstance().getAllPlayers()) {
			if (l1pc.getMapId()==(short)511) {
				_n++;
			}
		}
		if (_n==0) {
			return true;
		}
		return false;
	}
	
	public static List<Integer> _monObj = Lists.newList();	//生成的怪物objid列表
	
	private static int _readyTimer = 25;	//准备时间
	
	private static int _totalTimer = 1800;	//总的副本时间
	
	private static int _loopTimer = -10;	//循环计时器
	
	private static boolean _pauseSpawn = false;	//暂停刷怪
	
	public static boolean _endFlag = false;	//副本结束标志
	
	//副本阶段型完成检查
	public static int [] _stageClear = new int[3];
	
	private L1Object obj1 = null;
	
	private L1Object obj2 = null;
	
	private int stageClear() {
		if (_stageClear[0]==-1) {
			if (_gateId[0]!=-1 || _gateId[1]!=-1) { //阶段一脱出&下一阶段
				obj1 = L1World.getInstance().findObject(_gateId[0]);
				obj2 = L1World.getInstance().findObject(_gateId[1]);
				if (obj1!=null || obj2!=null) {
					clearMon();
					_stageClear[0] = 1;
					return 1;
				}
			}
			return 0;
		} else if (_stageClear[1]==-1 && _stageClear[0]==1) {
			if (_gateId[2]!=-1 || _gateId[3]!=-1) { //阶段一脱出&下一阶段
				obj1 = L1World.getInstance().findObject(_gateId[2]);
				obj2 = L1World.getInstance().findObject(_gateId[3]);
				if (obj1!=null || obj2!=null) {
					clearMon();
					_stageClear[1] = 1;
					return 2;
				}
			}
			return 0;
		} else if (_stageClear[2]==-1 && _stageClear[1]==1 && _stageClear[0]==1) {
			if (_gateId[4]!=-1 || _gateId[5]!=-1) { //阶段一脱出&下一阶段
				obj1 = L1World.getInstance().findObject(_gateId[4]);
				obj2 = L1World.getInstance().findObject(_gateId[5]);
				if (obj1!=null || obj2!=null) {
					clearMon();
					_stageClear[2] = 1;
					return 3;
				}
			}
			return 0;
		}
		return 0;
	}
	//检测队员人员是否传送完成
	private boolean playerEcape(int stage) {
		int count = 0;
		if (stage==1) {
			for (L1PcInstance pc : L1World.getInstance().getAllPlayers()) {
				if (pc.getMapId()==(short)511) {
					count++;
				}
			}
			if (count==0) {
				return true;
			}
		} else if (stage==2) {
			
		} else if (stage==3) {
			
		}
		return false;
	}
	
	//清除阶段
	private boolean clearStage(int stage) {
		if (playerEcape(stage)) {
			switch (stage) {
			case 1:
				L1Object obj = null;
				L1NpcInstance npc = null;
				for (int i=0; i<2; i++) {	//清除gate 0,1
					if (_gateId[i]!=-1) {
						obj = L1World.getInstance().findObject(_gateId[i]);
						if (obj!=null) {
							if (obj instanceof L1NpcInstance) {
								npc = (L1NpcInstance) obj;
								npc.deleteMe();
								_gateId[i] = -1;
							}
						}
					}
				}
				for (int i=0; i<_pc.size(); i++) {
					if (L1World.getInstance().findObject(_pc.get(i).getId())!=null) {
						for (L1ItemInstance it : _pc.get(i).getInventory().getItems()) {
							if (it.getItemId()>=70015 && it.getItemId()<=70018) {
								_pc.get(i).getInventory().removeItem(it, it.getCount());
							}
						}
					}
				}
				_c = 0;
				_loopTimer = 0;
				_n = 0;
				_pauseSpawn = false;
				_readyTimer = 25;
				_randomLoc.clear();
				_spawnLoc.clear();
				_totalTimer = 1800;
				_monObj.clear();
				i = 0;
				_bossCount = 0;
				tobj = null;
				tnpc = null;
				_pTimer = 9999999; //间隔进入时间300秒 测试用
				_endFlag = true; //测试用
//				_battleStart = false; //测试用
				break;
			case 2:
				break;
			case 3:
				break;
			default: 
				return false;
			}
			return true;
		}
		return false;
	}
	//清除怪物
	private static void clearMon() {
		L1Object obj = null;
		L1NpcInstance npc = null;
		for (int i=0; i<_monObj.size(); i++) {
			obj = L1World.getInstance().findObject(_monObj.get(i));
			if (obj!=null) {
				if (obj instanceof L1NpcInstance) {
					npc = (L1NpcInstance) obj;
					npc.deleteMe();
				}
			}
		}
	}
	//检查怪物数量
	private static L1Object tobj = null;
	
	private static L1NpcInstance tnpc = null;
	
	private void checkMonCount() {
		if (_monObj.size()>0) {
			for (int i=0; i<_monObj.size(); i++) {
				tobj = L1World.getInstance().findObject(_monObj.get(i));
				if (tobj==null) {
					_monObj.remove(_monObj.get(i));
				} else {
					if (tobj instanceof L1NpcInstance) {
						tnpc = (L1NpcInstance) tobj;
						if (tnpc.getCurrentHp()<=0 || tnpc.isDead()) {
							_monObj.remove(_monObj.get(i));
						}
					}
				}
			}	
		}
	}
	
	private static int _pTimer = -10;	//副本再次开始的延时
	
	@Override
	public void run() {
		while (true) {
			if (_pTimer > 0 && _battleOn && _endFlag) {	//副本被clear时进行再次进入延时 
				--_pTimer;
			} if (_pTimer<=0 && _pTimer>-10 && _battleOn && _endFlag) {
				_endFlag = false;
			} else {
				if (!checkTime() && _npcid!=0) {	//不是副本开放时间，移除欧瑞的副本npc
					removeNpc();
				}
				if (checkTime() && !_battleOn) {	//副本开放时间，在欧瑞生成npc
					_battleOn = true;	//可进入副本
					spwanNpc();	//生成npc
				}
				if (_battleOn && !_battleStart) {	//开始准备计时
					if (checkPlayerNum() && _readyTimer>0) {
						goback();
						//假如一进来就是用传送石，停止计时，清除副本
						if (stageClear()==1) {	//加入关卡被清除
							clearStage(1);	//清除阶段
							return;
						}
						--_readyTimer;	//40秒的准备时间
						if (_readyTimer==15) {
							for(int i=0; i<_pc.size(); i++) {
								_pc.get(i).sendPackets(new S_SystemMessage("剩余16秒...."));
							}
						} else if (_readyTimer<=5) {
							for(int i=0; i<_pc.size(); i++) {
								_pc.get(i).sendPackets(new S_SystemMessage("剩余"+_readyTimer+"秒...."));
							}
						}
						if (_readyTimer == 0) {	//开始副本，传送人员到指定位置
							for (L1PcInstance pc : L1World.getInstance().getAllPlayers()) {
								if (pc.getMapId()==(short)511) {
									L1Teleport.teleport(pc, 32608, 32765, (short) 511, 0, true);
								}
							}
							_readyTimer = -9;
						}
					} else if (_readyTimer<=0 && _readyTimer>-10) {	//发送消息
						for (int i=0; i<_pc.size(); i++) {
							if (_pc.get(i).getMapId()==(short)511) {
								_pc.get(i).sendPackets(new S_SystemMessage("感觉到黑暗开始侵袭...."));
							}
						}
						_battleStart = true;	//开始副本
						_readyTimer = -10;	//结束准备
//						renderLoc();	//生成随机位置
//						judgeLoc();	//判断玩家位置
//						spawnMon();	//生成怪物
//						_loopTimer = 25;	//延时25秒刷怪
						_loopTimer = 6;	//延时6秒刷怪
					}
				}
				if (_battleStart) {	//副本进行中
					goback();	//实时监测不合法的状况
					if (_totalTimer>0) {	//检查副本剩余时间
						/** 关卡清除及副本关闭监测 */
						if (stageClear()==1) {	//加入关卡被清除
							clearStage(1);	//清除阶段
							return;
						}
						if (closeFB()) {	//检查副本关闭条件
							clearFB();	//清除副本
						}
						/** END */
						
						/** 怪物数量实时更新 */
						checkMonCount();	//实时更新怪物数量
						bossCount();	//实时更新BOSS数量
						/** END */
						
						/** 副本时间控制 */
						if (checkInvalidLoc()) {	//实时检查玩家位置是否合法
							for(int i=0; i<_pc.size(); i++) {
								_pc.get(i).sendPackets(new S_SystemMessage("[注意]有队员在不允许进入的区域"));
							}
							_totalTimer -= 2;	//双倍时间流逝
						}
						//怪物超过10只 扣除副本时间 暂停刷怪
						else if (_monObj.size()>10 && !_pauseSpawn) {
							_pauseSpawn = true;
							for(int i=0; i<_pc.size(); i++) {
								_pc.get(i).sendPackets(new S_SystemMessage("[注意]怪物数量超出限制"));
							}
							_totalTimer = _totalTimer - 300; //减去5分钟
							_loopTimer = 180;	//暂停刷怪180秒
						}
						//BOSS数量超过2只
						else if (_bossCount>1) {
							for(int i=0; i<_pc.size(); i++) {
								_pc.get(i).sendPackets(new S_SystemMessage("[注意]BOSS数量超过两只"));
							}
							_totalTimer -= 3;	//三倍时间流逝
						}
						else {
							_totalTimer--;
						}
						//定时的通知
						if (_totalTimer<=300 && _totalTimer%60==0 && _totalTimer!=0) {
							for(int i=0; i<_pc.size(); i++) {
								_pc.get(i).sendPackets(new S_SystemMessage("副本时间剩余 "+(_totalTimer/60)+" 分钟...."));
							}
						} else if (_totalTimer<=10 && _totalTimer>=0) {
							for(int i=0; i<_pc.size(); i++) {
								_pc.get(i).sendPackets(new S_SystemMessage("副本时间剩余 "+_totalTimer+" 秒...."));
							}
						}
						/** END */
						
						/** 刷怪间隔控制 */
						if (_loopTimer>0) {
							--_loopTimer;
							if (_loopTimer == 5) {
								for(int i=0; i<_pc.size(); i++) {
									_pc.get(i).sendPackets(new S_SystemMessage("请勿随意走动...."));
								}
							}
						} else if (_loopTimer==0) {
							if (_pauseSpawn) {
								_pauseSpawn = false;
							}
							renderLoc();
							judgeLoc();
							if (_spawnLoc.size()>0) {
								spawnMon();
							}
							for(int i=0; i<_pc.size(); i++) {
								_pc.get(i).sendPackets(new S_SystemMessage("当前剩余怪物 "+_monObj.size()+" 只"));
							}
							//TODO:动态刷怪延时
//							_loopTimer = 25;	//延时25秒刷怪
							if (_monObj.size()<=2) {
								_loopTimer = 25;	//延时25秒刷怪
							} else if (_monObj.size()>2 && _monObj.size()<=4) {
								_loopTimer = 40;	//延时40秒刷怪
							} else if (_monObj.size()>4 && _monObj.size()<=6) {
								_loopTimer = 55;	//延时55秒刷怪
							} else if (_monObj.size()>6) {
								_loopTimer = 70;	//延时70秒刷怪
							}
						}
					} 
					/** END */
					
					/** 结束副本 */
					else if ((_totalTimer<=0 && !_endFlag) || (stageClear()==0)) {	//结束副本
						for(int i=0; i<_pc.size(); i++) {
							_pc.get(i).sendPackets(new S_SystemMessage("结束副本"));
						}
						_loopTimer = -10;
						_endFlag = true;
						endIt();
					}
					/** END */
				}
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
